"""
Test that when a projection from a volume is registered to the
volume that it converges to the right solution (best case registration scenario)

"""
import pythonRegistration as pr
from numpy import array, ones
import itk
import ImageCompare

reload(pr)

reg=pr.registration()
reg.verbose = False

fixedFileName = 'C:/Lib/JointTrack/branches/bryan/Images/ushortim080-LAT.tif'
volFileName = 'C:/Lib/jointtrack/branches/TestData/vert1.vtk'
calFileName = 'C:/Lib/JointTrack/branches/bryan/Tests/good_cal.txt'
saveFileName = fixedFileName[:-4]+'_out.png'

reg.SetFixedImageFileName(fixedFileName)
reg.SetVolumeFileName(volFileName)

#reg.SetCalibration(calFileName)

#Set the moving image to be black on white to match the fixed image
reg.SetBlackOnWhiteMovingImage()

metric = itk.MeanSquaresImageToImageMetric.IF2IF2.New()
reg.SetImageMetric(metric)

reg.SetWindowNames()

reg.InitializeRegistration()
#reg.movingImageInteractor.Disable()

pose = array((0,0,0,0,-90,0))
reg.SetPose(pose)
reg.StartInteraction()
#reg.movingImageRenWin.Render()

lowerBounds = -ones(6)+pose
upperBounds = ones(6)+pose

maxTime = 30
plotFlag = 1    #don't plot

x_opt, f_opt = reg.DoOptimize('galileo',lowerBounds,upperBounds,maxTime,plotFlag)

reg.SaveMovingImage(saveFileName)

#return 0 if they match, 1 otherwise.
comp = 0

#Test for positive match
assert comp==0

if comp == 0:
    print "Test Success!"
else:
    print "Test Failure!"

